package com.ruoyi.web.service;


import java.sql.Connection;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import com.ruoyi.web.utils.WebSocketUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageService {

    @Autowired
    private WebSocketUtil webSocketUtil;
    // 限制聊天记录数量
    final Integer limitMessagesLength = 6000;
    // 限制用户数量
    final Integer limitUserLength = 300;


    // 获取未读的接收信息
    /*public Integer findNoReadMessageLength(Connection conn, String userId) throws Exception {
        try {

            Integer totol = 0;
            AssertUtils.isError(StringUtils.isEmpty(userId), "用户编号不能为空!");
            UserDao userDao = new UserDao(conn);
            MessageDao messageDao = new MessageDao(conn);
            User user = userDao.selectbyUserId(userId);
            AssertUtils.isError(null == user, "用户编号:" + userId + "不存在!");
            // 为防止发送人特别多导致信息未获取，这里多设置一些拿信息数据
            List<Message> messages = messageDao
                    .selectByReceiveUserLimitLength(userId, limitMessagesLength);
            Map<String, List<Message>> messageBySendUserMap = messages.stream()
                    .collect(Collectors.groupingBy(Message::getSendUser));
            for (String sendUser : messageBySendUserMap.keySet()) {
                List<Message> receiveMessageList = messageBySendUserMap.get(sendUser);
                Integer noReadSize = receiveMessageList.stream().filter(o -> "0".equals(o.getIsRead()))
                        .collect(Collectors.toList()).size();
                totol += noReadSize;
            }
            return totol;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    // 发送信息的逻辑
    public void sendMessage(Connection conn, Message message) throws Exception {
        try {
            AssertUtils.isError(StringUtils.isEmpty(message.getSendUser()), "发送用户不能为空!");
            AssertUtils.isError(StringUtils.isEmpty(message.getReceiveUser()), "接收用户不能为空!");
            AssertUtils.isError(StringUtils.isEmpty(message.getContent()), "发送信息不能为空!");
            UserDao userDao = new UserDao(conn);
            MessageDao messageDao = new MessageDao(conn);
            User sendUser = userDao.selectbyUserId(message.getSendUser());
            AssertUtils.isError(null == sendUser, "发送用户不存在,发送信息失败!");
            AssertUtils.isError(sendUser.getStatus() != 1,
                    "发送用户:" + message.getSendUser() + "状态已冻结,无法发送信息!");
            User receiveUser = userDao.selectbyUserId(message.getReceiveUser());
            AssertUtils.isError(null == receiveUser, "接收用户不存在,发送信息失败!");
            AssertUtils.isError(receiveUser.getStatus() != 1,
                    "接收用户:" + message.getReceiveUser() + "状态已冻结,无法接收信息!");
            message.setHandle(UUID.randomUUID().toString());
            message.setIsRead("0");
            message.setCreateTime(LocalDateTime.now());
            messageDao.insert(message);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }
    // 获取两个人的聊天记录
    public List<Message> findMessageBySendUserAndReceiveUser(Connection conn, String sendUserId,
                                                             String receiveUserId) throws Exception {
        try {
            AssertUtils.isError(StringUtils.isEmpty(sendUserId), "发送用户为空!");
            AssertUtils.isError(StringUtils.isEmpty(receiveUserId), "接收用户为空!");
            UserDao userDao = new UserDao(conn);
            User sendUser = userDao.selectbyUserId(sendUserId);
            AssertUtils.isError(null == sendUser, "发送用户不存在,发送信息失败!");
            User receiveUser = userDao.selectbyUserId(receiveUserId);
            AssertUtils.isError(null == receiveUser, "接收用户不存在,发送信息失败!");
            MessageDao messageDao = new MessageDao(conn);
            // 获取对方发送的信息
            List<Message> receiveMessageList = messageDao.selectBySendUserAndReceiveUserLimitLength(
                    sendUserId,
                    receiveUserId, limitMessagesLength);
            // 获取发送给对方的信息
            List<Message> sendMessageList = messageDao.selectBySendUserAndReceiveUserLimitLength(
                    receiveUserId,
                    sendUserId, limitMessagesLength);
            List<Message> allMessageList = new ArrayList<>();
            allMessageList.addAll(receiveMessageList);
            allMessageList.addAll(sendMessageList);
            List<Message> sortedMessageList = allMessageList.stream()
                    .sorted(Comparator.comparing(Message::getCreateTime))
                    .collect(Collectors.toList());

            // 设置已读
            List<Message> noReadMessageList = receiveMessageList.stream()
                    .filter(o -> "0".equals(o.getIsRead()))
                    .peek(message -> message.setIsRead("1"))
                    .collect(Collectors.toList());
            if (noReadMessageList.size() > 0) {
                messageDao.update(noReadMessageList);
            }
            return sortedMessageList;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    // 获取所有数据
    public List<MessageForm> findAllMessageForm(Connection conn, String userId) throws Exception {
        try {

            Map<String, WebSocket> users = webSocketUtil.getUsers();
            Set<String> ids = users.keySet();
            AssertUtils.isError(StringUtils.isEmpty(userId), "用户编号不能为空!");
            List<MessageForm> messageFormList = new ArrayList<>();
            UserDao userDao = new UserDao(conn);
            MessageDao messageDao = new MessageDao(conn);
            User loginUser = userDao.selectbyUserId(userId);
            AssertUtils.isError(null == loginUser, "用户编号:" + userId + "不存在!");
            messageFormList.addAll(findAllMessageChatDataWithLoginUserId(conn, userId));
            // 判断ids是否在messageFormList的sendUser的Id中，不是则获取新的数据到messageFormList
            for (String id : ids) {
                if (!messageFormList.stream().map(o -> o.getSendUser().getUserId())
                        .collect(Collectors.toList())
                        .contains(id)) {
                    MessageForm messageForm = new MessageForm();
                    User sendUserData = userDao.selectbyUserId(id);
                    if (null == sendUserData) {
                        continue;
                    }
                    List<Message> allMessageList = findBothMessages(userId, id,
                            limitMessagesLength, messageDao);
                    messageForm.setMessages(allMessageList);
                    messageForm.setSendUser(sendUserData);
                    messageForm.setReceiveUser(loginUser);
                    messageForm.setIsOnline(true);
                    messageForm.setNoReadMessageLength(0);
                    messageForm.setLastMessage("");
                    messageFormList.add(messageForm);
                }
            }
            // 获取所有messageFormList的sendUser的userId,
            List<String> sendUserIds = messageFormList.stream().map(MessageForm::getSendUser)
                    .map(User::getUserId).collect(Collectors.toList());
            // 如果获取到的用户少于100个，则补齐剩余数量的用户数据,这里补齐的是从没和自己聊天过的用户
            if (sendUserIds.size() < limitUserLength) {
                Integer leaveCount = limitUserLength - sendUserIds.size();
                Where where = new Where();
                where.notIn("user_id", sendUserIds.toArray());
                where.limit(leaveCount);
                List<User> userList = userDao.selectByWhere(where);
                if (null != userList && userList.size() > 0) {
                    for (User user : userList) {
                        MessageForm messageForm = new MessageForm();
                        messageForm.setSendUser(user);
                        messageForm.setReceiveUser(loginUser);
                        messageForm.setIsOnline(false);
                        messageForm.setNoReadMessageLength(0);
                        messageForm.setLastMessage("");
                        messageFormList.add(messageForm);
                    }
                }
            }
            // 按照在线状态为true，有聊天记录的优先展示
            messageFormList.sort((o1, o2) -> {
                if (o1.getIsOnline() && o2.getIsOnline()) {
                    return o2.getMessages().size() - o1.getMessages().size();
                } else if (o1.getIsOnline()) {
                    return -1;
                } else if (o2.getIsOnline()) {
                    return 1;
                } else {
                    return o2.getMessages().size() - o1.getMessages().size();
                }
            });
            return messageFormList;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    // 获取登录用户所有聊过天的记录数据
    public List<MessageForm> findAllMessageChatDataWithLoginUserId(Connection conn, String userId)
            throws Exception {
        try {
            Map<String, WebSocket> users = webSocketUtil.getUsers();
            Set<String> ids = users.keySet();
            AssertUtils.isError(StringUtils.isEmpty(userId), "用户编号不能为空!");
            List<MessageForm> messageFormList = new ArrayList<>();
            UserDao userDao = new UserDao(conn);
            MessageDao messageDao = new MessageDao(conn);
            User loginUser = userDao.selectbyUserId(userId);
            AssertUtils.isError(null == loginUser, "用户编号:" + userId + "不存在!");
            // 获取所有有发送信息给自己聊天的用户
            List<String> allSendUsers = messageDao.selectByReceiveUser(userId).stream()
                    .map(Message::getSendUser).distinct().collect(Collectors.toList());
            for (String sendUser : allSendUsers) {
                // 发送人的用户信息
                MessageForm messageForm = new MessageForm();
                User sendUserData = userDao.selectbyUserId(sendUser);
                if (null == sendUserData) {
                    continue;
                }
                // 获取对方发送的信息
                List<Message> receiveMessageList = messageDao.selectBySendUserAndReceiveUserLimitLength(
                        sendUser,
                        userId, limitMessagesLength);
                // 获取发送给对方的信息
                List<Message> sendMessageList = messageDao.selectBySendUserAndReceiveUserLimitLength(
                        userId,
                        sendUser, limitMessagesLength);
                List<Message> allMessageList = new ArrayList<>();
                allMessageList.addAll(receiveMessageList);
                allMessageList.addAll(sendMessageList);

                List<Message> sortedMessageList = allMessageList.stream()
                        .sorted(Comparator.comparing(Message::getCreateTime))
                        .collect(Collectors.toList());
                // 赋值最新消息
                messageForm.setLastMessage(
                        sortedMessageList.size() > 0 ? sortedMessageList.get(sortedMessageList.size() - 1)
                                .getContent() : "");
                // 赋值聊天记录
                messageForm.setMessages(sortedMessageList);
                // 赋值未读消息
                messageForm.setNoReadMessageLength(
                        receiveMessageList.stream().filter(o -> "0".equals(o.getIsRead())).collect(
                                Collectors.toList()).size());
                // 赋值发送人
                messageForm.setSendUser(sendUserData);
                // 赋值接收人
                messageForm.setReceiveUser(loginUser);
                // 赋值是否在线
                messageForm.setIsOnline(ids.contains(sendUser));
                messageFormList.add(messageForm);
            }
            // 获取只有自己发送信息给别人的记录的用户
            List<String> allSendToUsers = messageDao.selectBySendUser(userId).stream()
                    .map(Message::getReceiveUser).distinct().collect(Collectors.toList());
            for (String receiveUser : allSendToUsers) {
                // 判断messageFormList的sendUser的userId是否包含receiveUser，有则说明已经存在了
                if (messageFormList.stream()
                        .anyMatch(o -> o.getSendUser().getUserId().equals(receiveUser))) {
                    continue;
                }
                MessageForm messageForm = new MessageForm();
                User receiveUserData = userDao.selectbyUserId(receiveUser);
                if (null == receiveUserData) {
                    continue;
                }
                messageForm.setReceiveUser(loginUser);
                messageForm.setSendUser(receiveUserData);
                messageForm.setLastMessage("");
                messageForm.setNoReadMessageLength(0);
                List<Message> sendMessageList = messageDao.selectBySendUserAndReceiveUserLimitLength(userId,
                        receiveUser, limitMessagesLength);
                // 按照CreateTime排序从小到大
                messageForm.setMessages(sendMessageList.stream()
                        .sorted(Comparator.comparing(Message::getCreateTime))
                        .collect(Collectors.toList()));
                messageForm.setIsOnline(ids.contains(receiveUser));
                messageFormList.add(messageForm);
            }
            return messageFormList;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    // 用户区查到的数据，有用户名就查用户名对应用户数据和聊天记录，没有默认查300个用户（配置）
    public List<MessageForm> searchUserForForm(Connection conn, String userId, String username)
            throws Exception {
        try {
            List<MessageForm> messageFormList = new ArrayList<>();
            AssertUtils.isError(StringUtils.isEmpty(userId), "登录用户不能为空!");
            UserDao userDao = new UserDao(conn);
            MessageDao messageDao = new MessageDao(conn);
            User loginUser = userDao.selectbyUserId(userId);
            AssertUtils.isError(null == loginUser, "登录用户不存在!");
            if (StringUtils.isNotEmpty(username)) {
                List<User> userList = userDao.selectByUserName(username);
                Map<String, WebSocket> users = webSocketUtil.getUsers();
                Set<String> ids = users.keySet();
                for (User user : userList) {
                    User sendUserData = userDao.selectbyUserId(user.getUserId());
                    if (null == sendUserData) {
                        continue;
                    }
                    MessageForm messageForm = new MessageForm();
                    messageForm.setSendUser(sendUserData);
                    messageForm.setReceiveUser(loginUser);
                    messageForm.setIsOnline(ids.contains(user.getUserId()));
                    // 获取对方发送的信息
                    List<Message> receiveMessageList = messageDao.selectBySendUserAndReceiveUserLimitLength(
                            sendUserData.getUserId(), loginUser.getUserId(), limitMessagesLength);
                    // 获取发送给对方的信息
                    List<Message> sendMessageList = messageDao.selectBySendUserAndReceiveUserLimitLength(
                            loginUser.getUserId(), sendUserData.getUserId(), limitMessagesLength);
                    List<Message> allMessages = new ArrayList<>();
                    allMessages.addAll(receiveMessageList);
                    allMessages.addAll(sendMessageList);
                    allMessages.sort((o1, o2) -> {
                        if (o1.getCreateTime().isBefore(o2.getCreateTime())) {
                            return -1;
                        } else if (o1.getCreateTime().isAfter(o2.getCreateTime())) {
                            return 1;
                        } else {
                            return 0;
                        }
                    });
                    if (allMessages.size() > 0) {
                        messageForm.setLastMessage(allMessages.get(allMessages.size() - 1).getContent());
                    } else {
                        messageForm.setLastMessage("");
                    }
                    messageForm.setMessages(allMessages);
                    // 获取allMessages中isRead为0的数量
                    messageForm.setNoReadMessageLength(
                            (int) allMessages.stream().filter(message -> "0".equals(message.getIsRead()))
                                    .count());
                    messageFormList.add(messageForm);
                }
            } else {
                messageFormList.addAll(findAllMessageForm(conn, userId));
            }
            return messageFormList;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    private List<Message> findBothMessages(String sendUserId, String receiveUserId,
                                           Integer limitMessageLength,
                                           MessageDao messageDao) {
        List<Message> receiveMessageList = messageDao.selectBySendUserAndReceiveUserLimitLength(
                receiveUserId,
                sendUserId, limitMessageLength);
        List<Message> sendMessageList = messageDao.selectBySendUserAndReceiveUserLimitLength(
                sendUserId,
                receiveUserId, limitMessageLength);
        List<Message> allMessageList = new ArrayList<>();
        allMessageList.addAll(receiveMessageList);
        allMessageList.addAll(sendMessageList);
        allMessageList.stream().sorted(Comparator.comparing(Message::getCreateTime))
                .collect(Collectors.toList());
        return allMessageList;
    }*/
}
